package js.web.center.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
/***
 * @author dowell
 * 
 * **/
public class JsWebHttpUtils {
	private static Logger logger=Logger.getLogger(JsWebHttpUtils.class);
	/**
	 * 发送get方法到服务器
	 * @param url 访问地址
	 * @param param 传递参数
	 * @return 返回信息
	 */
	public static String sendGet(String url, String param) {
		String result = "";
		BufferedReader in = null;
		try {
			String urlNameString = url + "?" + param;
			System.out.println(urlNameString);
			URL realUrl = new URL(urlNameString);
			URLConnection connection = realUrl.openConnection();
			connection.setRequestProperty("accept", "*/*");
			connection.setRequestProperty("connection", "Keep-Alive");
			connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
			connection.setRequestProperty("Charset", "UTF-8");
			connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			connection.connect();
			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			logger.error("JsWebHttpUtils ERROR:{}",e);
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
				logger.error("JsWebHttpUtils ERROR:{}",e2);
			}
		}
		return result;
	}
	public static String sendPost(String uri, String param, String charset,HashMap<String, String> headers) {
		String result = null;
		PrintWriter out = null;
		InputStream in = null;
		try {
			URL url = new URL(uri);
			HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
			urlcon.setDoInput(true);
			urlcon.setDoOutput(true);
			urlcon.setUseCaches(false);
			urlcon.setRequestMethod("POST");
			Iterator<Entry<String, String>> iterator = headers.entrySet().iterator();
			while(iterator.hasNext()){
				Entry<String, String> header = iterator.next();
				urlcon.setRequestProperty(header.getKey(), header.getValue());
			}
			urlcon.connect();// 获取连接
			out = new PrintWriter(urlcon.getOutputStream());
			out.print(param);
			out.flush();
			in = urlcon.getInputStream();
			BufferedReader buffer = new BufferedReader(new InputStreamReader(in, charset));
			StringBuffer bs = new StringBuffer();
			String line = null;
			while ((line = buffer.readLine()) != null) {
				bs.append(line);
			}
			result = bs.toString();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("JsWebHttpUtils ERROR:{}",e);
		} finally {
			try {
				if (null != in){
					in.close();
				}
				if (null != out){
					out.close();
				}
			} catch (Exception e2) {
				logger.error("JsWebHttpUtils ERROR:{}",e2);
			}
		}
		return result;
	}
	/**
	 * 发送post方法到服务器
	 * @param uri 消息发送地址
	 * @param param 参数
	 * @param charset 编码类型
	 * @return 返回信息
	 */
	public static String sendPost(String uri, String param, String charset) {
		String result = null;
		PrintWriter out = null;
		InputStream in = null;
		try {
			URL url = new URL(uri);
			HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
			urlcon.setDoInput(true);
			urlcon.setDoOutput(true);
			urlcon.setUseCaches(false);
			urlcon.setRequestMethod("POST");
			urlcon.connect();// 获取连接
			out = new PrintWriter(urlcon.getOutputStream());
			out.print(param);
			out.flush();
			in = urlcon.getInputStream();
			BufferedReader buffer = new BufferedReader(new InputStreamReader(in, charset));
			StringBuffer bs = new StringBuffer();
			String line = null;
			while ((line = buffer.readLine()) != null) {
				bs.append(line);
			}
			result = bs.toString();
		} catch (Exception e) {
			logger.error("JsWebHttpUtils ERROR:{}",e);
		} finally {
			try {
				if (null != in){
					in.close();
				}
				if (null != out){
					out.close();
				}
			} catch (Exception e2) {
				logger.error("JsWebHttpUtils ERROR:{}",e2);
			}
		}
		return result;
	}
}
